﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Songhay.Xml
{
    using Songhay.Models;

    /// <summary>
    /// Static members for XHTML Documents.
    /// </summary>
    public static class XhtmlDocumentUtility
    {
        /// <summary>
        /// XHTML Namespace
        /// </summary>
        public static XNamespace xhtml = "http://www.w3.org/1999/xhtml";

        /// <summary>
        /// Loads the document.
        /// </summary>
        /// <param name="pathToDocument">The path to document.</param>
        /// <param name="webPath">The public web path.</param>
        public static XhtmlDocument LoadDocument(string pathToDocument, string webPath)
        {
            var xd = XDocument.Load(pathToDocument);

            var heading =xd.Root
                .Element(xhtml+"body")
                .Element(xhtml+"h1");

            var title = xd.Root
                .Element(xhtml + "head")
                .Element(xhtml + "title")
                .Value;

            var d = new XhtmlDocument
            {
                Header = (heading == null) ? null : heading.Value,
                Location = webPath,
                Title = title
            };

            return d;
        }

        /// <summary>
        /// Writes the index of XHTML documents.
        /// </summary>
        /// <param name="indexFileName">Name of the index file.</param>
        /// <param name="indexTitle">The index title.</param>
        /// <param name="publicRoot">The public root.</param>
        /// <param name="pathToFolder">The path to folder.</param>
        /// <param name="pathToOutput">The path to output.</param>
        public static void WriteDocumentIndex(string indexFileName,
            string indexTitle, string publicRoot,
            string pathToFolder, string pathToOutput)
        {
            var directory = new DirectoryInfo(pathToFolder);
            var list = new List<XhtmlDocument>();
            directory.GetFiles()
                .ToList()
                .ForEach(f =>
                {
                    var uri = string.Concat(publicRoot, f.Name);
                    list.Add(XhtmlDocumentUtility.LoadDocument(f.FullName, uri));
                });

            var serializer = new XmlSerializer(typeof(XhtmlDocuments));
            var stream = new FileStream(string.Concat(pathToOutput, indexFileName), FileMode.Create);
            try
            {
                using(var writer = new XmlTextWriter(stream, Encoding.UTF8))
                {
                    var documents = new XhtmlDocuments
                    {
                        Documents = list.OrderBy(d => d.Title).ToArray(),
                        Title = indexTitle
                    };
                    serializer.Serialize(writer, documents);
                }
            }
            finally
            {
                if(stream != null) stream.Dispose();
            }
        }
    }
}
